<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : '{{ urlfor "ProductsController.DataGrid"}}'
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData);
			$('#search_key').val() ? params.filter = $('#search_key').val() : '';
			params.TypeId = defErpTypeid;

			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"data" : "Id", "bSortable": false, "sTitle":"ID"}
			,{"data" : "Number", "bSortable": false, "sTitle":"编码"}
            ,{render:function(data, type, full, meta) {
                    return '<span class="full_name_'+ full.Id +'">'+productGroupName(full)+'</span>';
                }, "sTitle":"全名"}
			,{"data" : "Name", "bSortable": false, "sTitle":"品名"}
            ,{"data":"Brand",render:function(data, type, full, meta) {
                    return full.BrandName;
             }, "sTitle":"品牌"}
			,{"bSortable": false, render:function(data, type, full, meta) {
				return '<a href="javascript:;" data-am-popover="{content: \''+ full.Suppliers.Name +'<br />联系人：'+ full.Suppliers.Contacts +'<br />电话：'+ full.Suppliers.Tel +'\', trigger: \'hover focus\'}">'+ full.Suppliers.Name +'</a>';
			}, "sTitle":"供应商"}
			,{"data" : "UpdatedAt", "bSortable": false, render:function(data, type, full, meta) {
				return timetrans(data);	
			}, "sTitle":"更新"}
			 ,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"15%",
				"sTitle":"操作",
				"render": function ( data, type, full, meta ) { 
					btns = '<a class="am-icon-edit" href="/product/edit/'+  full.Id +'" onclick="action(this);return false;"> 编辑</a>';
					if(full.Status == '2') {
						btns += '&nbsp;&nbsp;<a href="javascript:;"  class="am-icon-trash am-text-danger"  onclick="delProduct('+ full.Id +');return false;"> 启用</a>';
					} else {
						btns += '&nbsp;&nbsp;<a href="javascript:;" class="am-icon-trash"  onclick="delProduct('+ full.Id +');return false;"> 停用</a>';
					}
					if(full.TypeId != 50&&full.TypeId != 60&&full.TypeId != 70) {
                        btns += '&nbsp;&nbsp;<a href="javascript:;" onclick="viewSnd(\''+ full.Name +'\','+ full.Id +');return false;">参数列表</a>';
                    }
                    btns += '&nbsp;&nbsp;<a href="/product/price/'+  full.Id +'" data-id="'+ full.Id +'" onclick="actionPrice(this);return false;">价格设置</a>';
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		,"drawCallback": function(settings){
			$('[data-am-popover]').popover();
		}  
	});
	
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
	});

	//类别切换事件
	function defErpTypeidEvent(obj) {
        defErpTypeid= $(obj).data('id');
        $("#searchsubmit").click();
        showOpenSizeAndTitle();
    }
	
	function showOpenSizeAndTitle() {
	    var title = $('[data-id="'+ defErpTypeid+ '"]').html();
		switch(defErpTypeid) {
			case 10:
				return [['930px', '660px'], title];
			case 20:
                return [['930px', '660px'], title];
			case 50:
                return [['930px', '660px'], title];	
		}
        return [['930px', '500px'], title];
	}
    function viewSnd(title, id) {
        var uri = '{{ urlfor "ProductsController.Snd" }}?product_id='+id+'&typeid='+defErpTypeid;
        var title = $('.full_name_'+id).text();
        var index = layer.open({
            type: 2,
            title:title+' 参数列表',
            shadeClose: false,
            shade: 0.2,
            maxmin: false,
            shift: 1,
            area: ['1100px', '800px'],
            content: uri
        });
    }
	function action(obj) {
		var uri = $(obj).attr('href')+'?typeid='+defErpTypeid;
		var sizeAndTitle = showOpenSizeAndTitle();
		var index = layer.open({
            type: 2,
            title: sizeAndTitle[1] + '资料',
            shadeClose: false,
            shade: 0.2,
            maxmin: false,
            shift: 1,
            area: sizeAndTitle[0],
            content: uri
        });
	}

    function actionPrice(obj) {
        var uri = $(obj).attr('href')+'?typeid='+defErpTypeid;
        var title = $('.full_name_'+$(obj).data('id')).text();
        var index = layer.open({
            type: 2,
            title: title+ ' 价格列表',
            shadeClose: false,
            shade: 0.2,
            maxmin: false,
            shift: 1,
            area: ['90%', '90%'],
            content: uri
        });
        return false;
    }

	function delProduct(id) {
		layer.confirm('确定要操作所选记录吗？', function(index){
			$.post('{{ urlfor "ProductsController.Delete"}}', {id:id}, function(res){
				if(res.code == 1) {
					layer.msg(res.msg)
				} else {
					tables.ajax.reload();
				}
				layer.close(index);
			}, 'json');
		});
	}
	
	function dialogReback() {
		tables.ajax.reload();
	}
</script>